package com.itheima.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.R;
import com.itheima.dao.EmployeeDao;
import com.itheima.domain.Employee;
import com.itheima.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@Slf4j
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeDao, Employee> implements EmployeeService {
    @Autowired
    private EmployeeDao employeeDao;

    /**
     * 登陆
     *
     * @param req
     * @param employee
     * @return
     */
    @Override
    public R<Employee> login(HttpServletRequest req, Employee employee) {
        //添加查询条件
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<Employee>();
        lqw.eq(employee != null, Employee::getUsername, employee.getUsername());
        Employee emp = employeeDao.selectOne(lqw);
//        System.out.println(emp);

        //判断是否有该用户名
        if (emp == null) {
            return R.error("用户名错误");
        }

        //使用md5加密
        String password = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());

        //判断密码
        if (!password.equals(emp.getPassword())) {
            return R.error("密码错误");
        }

        //判断是否被禁用
        if (emp.getStatus() != 1) {
            return R.error("账户已禁用");
        }
        //设置Session
        HttpSession session = req.getSession();
        session.setAttribute("employee", emp.getId());
        log.info("session:{}", session.getAttribute("employee"));
        return R.success(emp);
    }

    @Override
    public R<String> SAVE(Employee employee, HttpServletRequest req) {
//        Long id = (Long) req.getSession().getAttribute("employee");
        //设置参数
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setCreateUser(id);
//        employee.setUpdateUser(id);
        //添加用户
        employeeDao.insert(employee);
        return R.success("添加成功");
    }

    /**
     * 分页查询
     *
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public Page pageService(Integer page, Integer pageSize, String name) {
        log.info("page:{} , pagesize:{}",page,pageSize);
        //创建page对象
        Page ipage = new Page(page, pageSize);
        //设置出查询条件
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.like(name != null, Employee::getName, name);
        //分页查询
        employeeDao.selectMapsPage(ipage, lqw);
        return ipage;
    }

}
